<script setup lang="ts">
import { useTaskStrategies } from "./utils/hook";
import { ref, onMounted, onBeforeUnmount } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { deviceDetection } from "@pureadmin/utils";
import EditPen from "@iconify-icons/ep/edit-pen";
import Delete from "@iconify-icons/ep/delete";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";
import View from "@iconify-icons/ep/view";
import { hasPerms } from "@/utils/auth";

defineOptions({
  name: "TaskStrategiesList"
});

const formRef = ref();
const tableRef = ref();
const {
  form,
  loading,
  columns,
  dataList,
  selectedNum,
  pagination,
  resetForm,
  onSearch,
  openDialog,
  handleEdit,
  handleDelete,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  onSelectionCancel,
  handleEnable,
  handlePreview,
  handleManualAssign,
  handleBatchExecute
} = useTaskStrategies(tableRef);

onMounted(() => {
  window.addEventListener('keyup', handleEnterKey);
});

onBeforeUnmount(() => {
  window.removeEventListener('keyup', handleEnterKey);
});

const handleEnterKey = (e) => {
  if (e.key === 'Enter' && formRef.value?.$el?.contains(e.target)) {
    onSearch();
  }
};
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="策略名称：" prop="strategy_name">
        <el-input
          v-model="form.strategy_name"
          placeholder="请输入策略名称"
          clearable
          class="!w-[150px]"
        />
      </el-form-item>

      <el-form-item label="状态：" prop="status">
        <el-select
          v-model="form.status"
          placeholder="请选择状态"
          clearable
          class="!w-[120px]"
        >
          <el-option label="启用" :value="1" />
          <el-option label="禁用" :value="0" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar title="任务策略管理" :columns="columns" @refresh="onSearch">
      <template #buttons>
        <div v-if="selectedNum > 0" class="w-full items-center">
          <span 
            style="font-size: var(--el-font-size-base)" 
            class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
          >
            已选 {{ selectedNum }} 项
          </span>
          <el-button type="primary" text @click="onSelectionCancel">
            取消选择
          </el-button>
          <el-popconfirm title="是否确认删除?" @confirm="handleDelete">
            <template #reference>
              <el-button type="danger" text class="mr-1">
                批量删除
              </el-button>
            </template>
          </el-popconfirm>
          <el-button
            type="success"
            text
            class="mr-1"
            @click="handleBatchExecute"
          >
            批量手动执行
          </el-button>
        </div>

        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog()"
        >
          新建策略
        </el-button>
        <!-- <el-button
          type="success"
          :loading="loading"
          @click="handleManualAssign"
        >
          触发自动策略
        </el-button> -->
      </template>

      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          align-whole="center"
          table-layout="auto"
          :loading="loading"
          :size="size"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="pagination"
          :paginationSmall="size === 'small'"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          row-key="id"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="info"
              :size="size"
              :icon="useRenderIcon(View)"
              @click="handlePreview(row)"
            >
              预览
            </el-button>
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-button
              class="reset-margin"
              link
              :type="row.status === 1 ? 'warning' : 'success'"
              :size="size"
              @click="handleEnable(row)"
            >
              {{ row.status === 1 ? '禁用' : '启用' }}
            </el-button>
            <el-popconfirm
              :title="`确认删除策略${row.strategy_name}吗？`"
              @confirm="handleDelete(row)"
              width="200"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style> 